<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
        <el-form-item label="项目名称" prop="pro_name">
          <el-input
            v-model="queryParams.pro_name"
            placeholder="请输入项目名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="企业名称" prop="configKey">
          <el-input
            v-model="queryParams.jlr_name"
            placeholder="请输入企业名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">

        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:config:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
        <el-table-column label="项目名称" align="center" prop="pro_name">
          <template slot-scope="scope">
            <FinancePro :row="scope.row"/>
          </template>
        </el-table-column>
        <el-table-column label="平台服务金额" width="100" align="center" prop="plat_fee_total" :show-overflow-tooltip="true" />
        <el-table-column label="牵头人报警" align="center" prop="configKey" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <div class="flex_bj">
              <span>  {{ scope.row.qt_price_time }} </span> <span> {{  scope.row.qt_price_num + '/' + scope.row.payment_num }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="经理人报警" align="center" prop="configValue" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <div class="flex_bj">
              <span>  {{ scope.row.jlr_price_time }} </span> <span> {{  scope.row.jlr_price_num + '/' + scope.row.payment_num }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="确认打款次数" width="100" align="center" prop="configValue" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <el-select size="mini" v-model="scope.row.confirm_price_num" @change="changeNum($event,scope.row)" placeholder="请选择">
              <el-option
                v-for="item in financeNum"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="打款金额" align="center" prop="payment_price_arr" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            {{ scope.row.payment_price_arr[scope.row.confirm_price_num] }}
          </template>
        </el-table-column>
        <el-table-column label="分配明细" align="center" prop="" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
              <el-button
                slot="reference"
                type="success"
                plain
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                size="mini">查看</el-button>

            </template>
        </el-table-column>
        <el-table-column label="打款状态" width="200" align="center" prop="confirm_price_state" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-radio-group size="mini" @input="changeMethod($event,scope.row)" v-model="scope.row.confirm_price_state">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">异常</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.page_size"
        @pagination="getList"
      />
  
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="中标日期" prop="bid_date">
           <span>{{form.bid_date}}</span>
          </el-form-item>
          <el-form-item label="中标金额" prop="configKey">
            <span>{{form.bid_price}}</span>
          </el-form-item>
          <el-form-item label="服务费" prop="configValue">
            <span>{{ form.plat_fee_total }}</span>
          </el-form-item>
          <el-form-item label="明细" prop="configType">
            <el-table :data="form.service_price_list">
                <el-table-column  property="type_name" label="角色分类" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column  property="title" label="角色名称" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column  property="member_bank.name" label="打款账户"></el-table-column>
                <el-table-column  property="member_bank.phone" label="电话" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column  property="member_bank.bank_card_number" label="打款账号" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column  property="member_bank.deposit_bank" label="所属银行"></el-table-column>
                <el-table-column  property="rate" label="分配比例"></el-table-column>
                <el-table-column  property="price" label="分配总金额"></el-table-column>
                <el-table-column  property="address" label="金额">
                  <template slot-scope="scope">
                    {{scope.row.price_arr[scope.row.jiesuan]}}
                  </template>
                </el-table-column>
                <el-table-column  property="address" label="实际打款次数">
                  <template slot-scope="scope">
                    <el-select size="mini" v-model="scope.row.jiesuan" @change="changeNumOther($event,scope.row)" placeholder="请选择">
                      <el-option
                        v-for="item in financeNum"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
              </el-table>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { service_list,service_handler_price_state,service_price_list,service_price_change_num,service_handler_price_num } from "@/api/finance/index";
  import { financeNum } from '@/utils/generator/config'
  import  FinancePro  from '@/components/finance/index.vue'
  export default {
    name: "serviceIndex",
    components: { FinancePro },
    data() {
      return {
        //财务打开次数
        financeNum: financeNum,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 参数表格数据
        configList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          page: 1,
          page_size: 10,
          configName: undefined,
          configKey: undefined,
          configType: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询参数列表 */
      getList() {
        this.loading = true;
        service_list(this.addDateRange(this.queryParams, this.dateRange,'-s')).then(response => {
            this.configList = response.data.list;
            this.total = response.data.totalCount;
            this.loading = false;
          }
        );
      },
      showContent(row) {
        service_price_list({bind:row.bin,eid:row.eid}).then((res)=>{
          console.log(res,'---res')
        })
        //请求数据明细

      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          configId: undefined,
          configName: undefined,
          configKey: undefined,
          configValue: undefined,
          configType: "Y",
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加参数";
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.configId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const formData = {
          bid_date: row.bid_date,
          bid_price: row.bid_price,
          plat_fee_total: row.plat_fee_total,
          }
        this.open = true;
        this.title = "服务费分配";
        if(!row.service_price_list) {
          service_price_list({eid:row.eid,bid:row.bid}).then(response => {
            formData.service_price_list = response.data;
            this.form = formData;
          });
        }
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.configId != undefined) {
              updateConfig(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addConfig(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const configIds = row.configId || this.ids;
        this.$modal.confirm('是否确认删除参数编号为"' + configIds + '"的数据项？').then(function() {
            return delConfig(configIds);
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('admin/finance/service_excel_export', {
          ...this.queryParams
        }, `config_${new Date().getTime()}.xlsx`)
      },
      /** 刷新缓存按钮操作 */
      handleRefreshCache() {
        refreshCache().then(() => {
          this.$modal.msgSuccess("刷新成功");
        });
      },
      // 修改打款状态
      changeMethod(value,row) {
        service_handler_price_state({'bid':row.bid,'confirm_price_state': value}).then(res =>{
          this.$message.success( row.pro_name + '打款状态修改成功')
        })
      },
      // 修改打款次数
      changeNum(value,row) {
        console.log(value,'---value')
        service_handler_price_num({'bid':row.bid,'price_num': value}).then(res =>{
          this.$message.success( row.pro_name + '打款次数更新成功')
        })
      },
      // 给别人打款次数更新
      changeNumOther(value,row) {
        service_price_change_num({'id':row.id,'jiesuan': value}).then(res =>{
          this.$message.success( '打款次数更新成功')
        })
      },
    }
  };
  </script>

<style scoped lang="scss">
  .flex_bj{
    display: flex;
    justify-content: space-between;
  }
</style>